import React from 'react';
import { render } from 'react-dom';
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Redirect
} from 'react-router-dom';
import zhCN from 'antd/es/locale/zh_CN';
import { ConfigProvider } from 'antd';
import { Provider } from 'react-redux';
import store from './store';
import { mainRoutes } from './routes'; //mainRoutes里面的理由是和App同级的
import App from './App';
import './index.less';
render(
  //nbfee：设置样式前缀
  <Provider store={store}>
    <ConfigProvider locale={zhCN}>
      <Router>
        <Switch>
          <Route
            path="/admin"
            render={routeProps => {
              //如果没有登录跳转到登录页
              return store.getState().User.isLogin ? (
                <App {...routeProps} />
              ) : (
                <Redirect to="/login" />
              );
            }}
          />
          {mainRoutes.map(route => {
            return (
              <Route
                key={route.pathname}
                path={route.pathname}
                component={route.component}
              />
            );
          })}
          <Redirect to="/admin" from="/" exact />
          {/**这里需要精确匹配 */}
          {/**如果什么路由都没有匹配到，404 */}
          <Redirect to="/404" />
        </Switch>
      </Router>
    </ConfigProvider>
  </Provider>,
  document.getElementById('root')
);
